<?php

class BannersController extends Controller
{
	/**
	 * Declares class-based actions.
	 */
    	 public function filters()
	{
		return array(
			'accessControl',
		);
	}

	public function accessRules()
	{
		return array(
			array('allow',
			      'actions' => array('login'),
			      'users' => array('*'),
			),
			array('allow', // allow admin user to perform 'admin' and 'delete' actions
				'actions'=>array('add','index','edit','delete','category','add_cat','edit_cat','delete_cat'),
				'roles'=>array('admin'),
			),
			array('deny',
			      'users' => array('*'),
			),
		);
	}

	public function actions()
	{
		return array(
			// captcha action renders the CAPTCHA image displayed on the contact page
			'captcha'=>array(
				'class'=>'CCaptchaAction',
				'backColor'=>0xFFFFFF,
			),
			'page'=>array(
				'class'=>'CViewAction',
			),
		);
	}

	public function actionIndex()
	{
        if(isset($_POST['article-grid_c0'])){
           $this->deleteAll($_POST['article-grid_c0']);
        }
		$this->pagetitle = 'Баннеры - '.Yii::app()->name;
		$this->render('index', array('model'=>Banner::model()));
    }

    public function actionAdd()
    {
        $model = new Banner;
        if(isset($_POST['ajax']) && $_POST['ajax']==='addnews-form')
            {
                echo CActiveForm::validate($model);
                Yii::app()->end();
            }
        if(isset($_POST['Banner']))
        {
            $model->setScenario('add');
            $model->attributes=$_POST['Banner'];
            $model->file=CUploadedFile::getInstance($model,'file');
            if($model->file){
                $temp = pathinfo($model->file);
                $model->source_file = uniqid().'.'.strtolower($temp['extension']);
            }

            if($model->save())
            {
                $model->file->saveAs(Yii::getPathOfAlias('webroot.banners') .'/' . $model->source_file);
                $this->redirect(Yii::app()->creatUrl('banners'));
            }
        }
        $this->render('create', array('model' => $model));
    }

    public function actionEdit($id)
    {
        $cfile = false;
        $model = Banner::model()->findByPk($id);

        if(isset($_POST['Banner']))
        {
            $model->setScenario('mnf');
            $model->attributes=$_POST['Banner'];
            $model->file=CUploadedFile::getInstance($model,'file');
            if($model->file){
                $cfile = true;
                $temp = pathinfo($model->file);
                $model->source_file = uniqid().'.'.strtolower($temp['extension']);
            }

            if($model->save())
            {
                if($cfile)
                {
                    $model->file->saveAs(Yii::getPathOfAlias('webroot.banners') .'/' . $model->source_file);
                }
                $this->redirect(Yii::app()->createUrl('banners'));
            }
        }
        $this->render('edit', array('model' => $model));
    }

    public function actionDelete($id)
    {
        $model = Banner::model()->findByPk($id);
        if(unlink(Yii::getPathOfAlias('webroot.banners') .'/' . $model->source_file))
        {
            $model->delete();
        }

        $this->redirect(Yii::app()->creatUrl('banners'));

    }

    private function deleteAll($ids)
    {
        foreach($ids as $id){
            $model = Banner::model()->findByPk($id);
            if(unlink(Yii::getPathOfAlias('webroot.banners') .'/' . $model->source_file))
            {
                $model->delete();
            }
        }
    }


}